package com.chainyoung.component_tmperson.ui.mvp.presenter;


import android.text.TextUtils;

import com.chainyoung.common.appcommon.entity.RecieveAccount;
import com.chainyoung.common.base.CommonSubscriber;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.di.scope.ActivityScope;
import com.chainyoung.common.exception.ApiException;
import com.chainyoung.common.mvp.BasePresenter;
import com.chainyoung.common.utils.RxUtils;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.component_tmperson.entity.Bank;
import com.chainyoung.component_tmperson.ui.mvp.contract.RecieveAccountListContract;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import javax.inject.Inject;

import static com.chainyoung.common.constant.LibConstant.NO_RESULT_ERRORCODE;

@ActivityScope
public class RecieveAccountListPresenter extends BasePresenter<RecieveAccountListContract.Model,RecieveAccountListContract.View>{

    @Inject
    public RecieveAccountListPresenter(RecieveAccountListContract.Model model, RecieveAccountListContract.View rootView) {
        super(model, rootView);
    }




    public void getRecieveAccount(TreeMap<String,Object> map){
        addSubscribe(mModel.getRecieveAccount(map)
                .compose(RxUtils.<List<RecieveAccount>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<RecieveAccount>>(mView,true) {
                    @Override
                    public void onNext(List<RecieveAccount> data) {
                        mView.onGetRecieveAccount(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException){
                            if (((ApiException) e).getErrorCode().equals(NO_RESULT_ERRORCODE)){
                                mView.onGetRecieveAccount(new ArrayList<RecieveAccount>());
                                mView.hideLoading();
                                return;
                            }
                        }
                        super.onError(e);
                    }
                }));
    }

    public void setRecieveAccountStatus(TreeMap<String,Object> map){
        addSubscribe(mModel.setRecieveAccountStatus(map)
                .compose(RxUtils.<String>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<String>(mView) {
                    @Override
                    public void onNext(String data) {
                        mView.onSetRecieveAccountStatus(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));
    }
}
